package com.lemt.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lemt.mall.entity.Goods;
import com.lemt.mall.entity.Inventory;
import com.lemt.mall.entity.ShoppingCart;
import com.lemt.mall.entity.vo.GoodsVo;
import com.lemt.mall.entity.vo.ShoppingCartVo;
import com.lemt.mall.mapper.GoodsMapper;
import com.lemt.mall.mapper.InventoryMapper;
import com.lemt.mall.mapper.ShoppingCartMapper;
import com.lemt.mall.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.springframework.beans.BeanUtils.copyProperties;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zik
 * @since 2023-09-10
 */
@Slf4j
@Service(value ="ShoppingCartService")
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Resource
    private InventoryMapper inventoryMapper;

    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public Boolean saveShoppingCart(ShoppingCart shoppingCart) {
        //购物车添加不需要多余的判定，直接往里面怼
        if(shoppingCartMapper.insert(shoppingCart) == 1) {
            return true;
        }
        return false;
    }

    @Override
    public Map<String,Object> getCartPage(Integer page, Integer pageSize, String name,Integer userId) {

        log.info("name--》",name);

        log.info("查询到对应的购物车信息");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.like(Strings.isEmpty(name),ShoppingCart :: getGoodsName ,name).eq(ShoppingCart::getUserId,userId).orderByDesc(ShoppingCart::getCreateTime);


        Page<ShoppingCart> shoppingCartPage = shoppingCartMapper.selectPage(new Page<>(page, pageSize),shoppingCartLambdaQueryWrapper);

        long total = shoppingCartPage.getTotal();
        //总页数
        long totalPage = shoppingCartPage.getPages();
        //当前页
        long currentPage = shoppingCartPage.getCurrent();
        //每页记录数
        long size = shoppingCartPage.getSize();
        //每页数据集合
        List<ShoppingCart> shoppingCartList = shoppingCartPage.getRecords();

        ArrayList<ShoppingCartVo> shoppingCartVos = new ArrayList<>();
        log.info("封装对应的购物车Vo");
        shoppingCartList.forEach(item -> {

            ShoppingCartVo shoppingCartVo = new ShoppingCartVo();
            copyProperties(item,shoppingCartVo);


            Inventory inventory = inventoryMapper.selectOne(new LambdaQueryWrapper<Inventory>().eq(Inventory::getId, item.getInventoryId()));
            log.info("获取库存信息{}",inventory);

            Goods goods = goodsMapper.selectOne(new LambdaQueryWrapper<Goods>().eq(Goods::getId, item.getGoodsId()));
            log.info("获取商品信息{}",goods);
            shoppingCartVo.setGoods(goods);
            shoppingCartVo.setInventory(inventory);
            log.info("shoppingVo={}",shoppingCartVo);
            shoppingCartVos.add(shoppingCartVo);
        });
        log.info("购物车vo=》 {}",shoppingCartVos);

        //封装返回数据
        Map<String,Object> map = new HashMap<>();
        map.put("totalCount",total);
        map.put("totalPage",totalPage);
        map.put("currentPage",currentPage);
        map.put("size",size);
        map.put("records",shoppingCartVos);
        return map;
    }
}
